package com.sgcc.fj.pmos.mobileapp.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.sgcc.fj.pmos.arouter.app.AppArouterPath;
import com.sgcc.fj.pmos.common.adapter.HomeTabPagerAdapter;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.base.MvpBaseActivity;
import com.sgcc.fj.pmos.common.utils.ImmersionBarUtils;
import com.sgcc.fj.pmos.common.utils.UserManager;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.fj.pmos.e_fj_mall.widget.NoScrollViewPager;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.mobileapp.fragment.AccountNoInfoFragment;
import com.sgcc.fj.pmos.mobileapp.fragment.BankInfoFragment;
import com.sgcc.fj.pmos.mobileapp.fragment.BusinessInfoFragment;
import com.sgcc.fj.pmos.mobileapp.fragment.ContactInfoFragment;
import com.sgcc.fj.pmos.mobileapp.fragment.ContactPersonInfoFragment;
import com.sgcc.fj.pmos.mobileapp.fragment.LegalPersonInfoFragment;
import com.sgcc.fj.pmos.mobileapp.fragment.SubmitSuccessFragment;
import com.sgcc.fj.pmos.mobileapp.utils.OnNextListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 关联企业信息
 */
@Route(path = AppArouterPath.APP_ASSOCIATEDENTERPRISEINFO)
public class AssociatedEnterpriseInfoActivity extends MvpBaseActivity {

    private TitleBar titleBar;
    private TextView tvIndex;
    private TextView tvFragmentName;
    private NoScrollViewPager nsvpLayout;
    private TextView tvLast;
    private TextView tvNext;
    private TextView tvSubmit;
    private LinearLayout llBtnLayout;
    private LinearLayout llInfoLayout;

    @Autowired
    public String position;//跳转第几步

    private int index = 0;//当前第几页
    private List<Fragment> fragments = new ArrayList<>();
    private String[] nameStr = new String[]{"工商信息", "法人信息", "联系信息",
            "联系人信息", "户号信息", "银行信息", ""};
    private BusinessInfoFragment businessInfoFragment;
    private LegalPersonInfoFragment legalPersonInfoFragment;
    private ContactInfoFragment contactInfoFragment;
    private ContactPersonInfoFragment contactPersonInfoFragment;
    private AccountNoInfoFragment accountNoInfoFragment;
    private BankInfoFragment bankInfoFragment;

    private String legalReprName;
    private String regAddress;
    private SubmitSuccessFragment submitSuccessFragment;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_associatedenterpriseinfo;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ImmersionBarUtils.initTrans1lBar(this);
        titleBar = (TitleBar) findViewById(R.id.title_bar);

        tvIndex = findViewById(R.id.tvIndex);
        llInfoLayout = findViewById(R.id.llInfoLayout);
        llBtnLayout = findViewById(R.id.llBtnLayout);
        tvFragmentName = findViewById(R.id.tvFragmentName);
        nsvpLayout = findViewById(R.id.nsvpLayout);
        tvLast = findViewById(R.id.tvLast);
        tvNext = findViewById(R.id.tvNext);
        tvSubmit = findViewById(R.id.tvSubmit);
        titleBar.setTitle("关联企业信息");
        titleBar.onBackClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserManager.logout();
                finish();
            }
        });
        titleBar.ivBack.setImageResource(R.mipmap.ic_close1);
        titleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserManager.logout();
                goMainActivity();
            }
        });
    }


    @Override
    protected void initData() {
        super.initData();

        //-1实名认证，空字符串为工商信息，1完成法定代表人，
        // 2联系信息，3联系人信息，4户号信息，
        // 5银行信息
        if (TextUtils.isEmpty(position)) {
            index = 0;
        } else {
            int i = Integer.parseInt(position);
            if (i == -1) {
                index = 0;
            } else {
                index = i;
            }
        }


        businessInfoFragment = BusinessInfoFragment.newInstance();
        legalPersonInfoFragment = LegalPersonInfoFragment.newInstance();
        contactInfoFragment = ContactInfoFragment.newInstance();
        contactPersonInfoFragment = ContactPersonInfoFragment.newInstance();
        accountNoInfoFragment = AccountNoInfoFragment.newInstance();
        bankInfoFragment = BankInfoFragment.newInstance();
        submitSuccessFragment = SubmitSuccessFragment.newInstance();

        fragments.add(businessInfoFragment);
        fragments.add(legalPersonInfoFragment);
        fragments.add(contactInfoFragment);
        fragments.add(contactPersonInfoFragment);
        fragments.add(accountNoInfoFragment);
        fragments.add(bankInfoFragment);
        fragments.add(submitSuccessFragment);

        HomeTabPagerAdapter homeTabPagerAdapter = new HomeTabPagerAdapter(getSupportFragmentManager(), fragments);
        nsvpLayout.setAdapter(homeTabPagerAdapter);
        nsvpLayout.setOffscreenPageLimit(fragments.size());
        nsvpLayout.setCurrentItem(index, false);


        tvNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (index) {
                    case 0:
                        businessInfoFragment.next(new OnNextListener() {
                            @Override
                            public void onNext(boolean isNext) {
                                if (!isNext) {
                                    return;
                                }
                                nextPage();

                            }
                        });


                        break;
                    case 1:
                        legalPersonInfoFragment.next(new OnNextListener() {
                            @Override
                            public void onNext(boolean isNext) {
                                if (!isNext) {
                                    return;
                                }
                                contactInfoFragment.getData();
                                nextPage();

                            }
                        });

                        break;
                    case 2:
                        contactInfoFragment.next(new OnNextListener() {
                            @Override
                            public void onNext(boolean isNext) {
                                if (!isNext) {
                                    return;
                                }
                                nextPage();

                            }
                        });
                        break;
                    case 3:
                        if (contactPersonInfoFragment.next()) {
                            nextPage();
                        }
                        break;
                    case 4:
                        if (accountNoInfoFragment.next()) {
                            nextPage();
                        }
                        break;
                    case 5:
                        bankInfoFragment.next();
                        break;
                }


            }
        });
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (accountNoInfoFragment.next()) {
                    bankInfoFragment.save("", "", "");
                }
            }
        });

        tvLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                index -= 1;
                nsvpLayout.setCurrentItem(index, false);
                setTopText();

            }
        });
        setTopText();
    }

    public void submitSuccess(int index) {
        nsvpLayout.setCurrentItem(fragments.size() - 1, false);
        llInfoLayout.setVisibility(View.GONE);
        llBtnLayout.setVisibility(View.GONE);
    }


    private void nextPage() {
        index += 1;
        nsvpLayout.setCurrentItem(index, false);
        setTopText();
    }

    private void setTopText() {
        String num = (index + 1) + "/6";
        tvIndex.setText(num);
        tvFragmentName.setText(nameStr[index]);
        tvLast.setVisibility(index == 0 ? View.GONE : View.VISIBLE);
        tvSubmit.setVisibility(index == 4 ? View.VISIBLE : View.GONE);
        tvNext.setText(index == 5 ? "提交" : "下一步");

    }


    public String getLegalReprName() {
        return legalReprName;
    }

    public void setLegalReprName(String legalReprName) {
        this.legalReprName = legalReprName;
    }

    public String getRegAddress() {
        return TextUtils.isEmpty(regAddress) ? "" : regAddress;
    }

    public void setRegAddress(String regAddress) {
        this.regAddress = regAddress;
    }
}
